<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { PropType, reactive, ref } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { SysMachineReportDetailVo } from '@/api/system/monitor/SysMachineReport/types'
import { isBlank } from '@/utils/StringUtils'
import { useBaseWriteView } from '@/base/views/BaseWriteView'
import { sysDictChooseListApi } from '@/api/system/dict/SysDict'
import { KeyLabelValueVo } from '@/base/api/KeyLabelValueVo'
// [[ OnflowGenerator Range (- selectFunctionImport -) Start ]]
import { selectSysMachineInfoMachineIdListApi } from '@/api/system/monitor/SysMachineInfo'
// [[ OnflowGenerator Range (- selectFunctionImport -) End ]]

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<SysMachineReportDetailVo>>,
    default: () => null
  },
  editType: {
    type: String,
    default: 'add'
  },
  editId: {
    type: Number,
    default: -1
  }
})
const isAdd = ref(props.editType === 'add')

const { formRegister, submit, doWatch } = useBaseWriteView(props)
doWatch()

const formRef = ref<typeof Form>()
// [[ OnflowGenerator Range (- dictFunctions -) Start ]]

// [[ OnflowGenerator Range (- dictFunctions -) End ]]

// [[ OnflowGenerator Range (- selectFunctions -) Start ]]
const sysMachineInfoMachineIdList = ref<Array<KeyLabelValueVo>>([])
const getSysMachineInfoMachineIdList = async () => {
  const res = await selectSysMachineInfoMachineIdListApi({})
  sysMachineInfoMachineIdList.value = res.data
}
getSysMachineInfoMachineIdList()

// [[ OnflowGenerator Range (- selectFunctions -) End ]]

const schema = reactive<FormSchema[]>([
  // [[ OnflowGenerator Range (- inputSchema -) Start ]]
  {
    field: 'infoId',
    label: '服务器信息',
    component: 'Select',
    componentProps: {
      placeholder: '请选择服务器信息Id',
      clearable: true,
      filterable: true,
      options: sysMachineInfoMachineIdList
    }
  },
  {
    field: 'machineId',
    label: '服务器id',
    component: 'Input',
    componentProps: {
      placeholder: '请输入服务器id',
      clearable: true
    }
  },
  {
    field: 'cpuUsage',
    label: 'CPU使用率',
    component: 'InputNumber',
    componentProps: {
      placeholder: '请选择CPU使用率',
      clearable: true
    }
  },
  {
    field: 'cpuFreq',
    label: 'CPU频率',
    component: 'InputNumber',
    componentProps: {
      placeholder: '请选择CPU频率',
      clearable: true
    }
  },
  {
    field: 'memoryUsage',
    label: '内存使用率',
    component: 'InputNumber',
    componentProps: {
      placeholder: '请选择内存使用率',
      clearable: true
    }
  },
  {
    field: 'physicalMemoryInUse',
    label: '物理内存使用量',
    value: '0',
    component: 'InputNumber',
    componentProps: {
      placeholder: '请选择物理内存使用量',
      clearable: true
    }
  },
  {
    field: 'physicalMemoryTotal',
    label: '物理内存总量',
    value: '0',
    component: 'InputNumber',
    componentProps: {
      placeholder: '请选择物理内存总量',
      clearable: true
    }
  },
  {
    field: 'virtualMemoryInUse',
    label: '虚拟内存使用量',
    value: '0',
    component: 'InputNumber',
    componentProps: {
      placeholder: '请选择虚拟内存使用量',
      clearable: true
    }
  },
  {
    field: 'virtualMemoryTotal',
    label: '虚拟内存总量',
    value: '0',
    component: 'InputNumber',
    componentProps: {
      placeholder: '请选择虚拟内存总量',
      clearable: true
    }
  },
  {
    field: 'storageUsage',
    label: '存储使用率',
    component: 'InputNumber',
    componentProps: {
      placeholder: '请选择存储使用率',
      clearable: true
    }
  },
  {
    field: 'storageTotal',
    label: '存储总量',
    value: '0',
    component: 'InputNumber',
    componentProps: {
      placeholder: '请选择存储总量',
      clearable: true
    }
  },
  {
    field: 'storageInUse',
    label: '存储使用量',
    value: '0',
    component: 'InputNumber',
    componentProps: {
      placeholder: '请选择存储使用量',
      clearable: true
    }
  },
  {
    field: 'diskWrite',
    label: '磁盘写入',
    value: '0',
    component: 'InputNumber',
    componentProps: {
      placeholder: '请选择磁盘写入',
      clearable: true
    }
  },
  {
    field: 'diskRead',
    label: '磁盘读取',
    value: '0',
    component: 'InputNumber',
    componentProps: {
      placeholder: '请选择磁盘读取',
      clearable: true
    }
  },
  {
    field: 'networkDownload',
    label: '网络下载',
    value: '0',
    component: 'InputNumber',
    componentProps: {
      placeholder: '请选择网络下载',
      clearable: true
    }
  },
  {
    field: 'networkUpload',
    label: '网络上传',
    value: '0',
    component: 'InputNumber',
    componentProps: {
      placeholder: '请选择网络上传',
      clearable: true
    }
  },
  {
    field: 'jvmMemoryUsage',
    label: 'JVM内存使用率',
    component: 'InputNumber',
    componentProps: {
      placeholder: '请选择JVM内存使用率',
      clearable: true
    }
  },
  {
    field: 'jvmMemoryTotal',
    label: 'JVM内存总量',
    value: '0',
    component: 'InputNumber',
    componentProps: {
      placeholder: '请选择JVM内存总量',
      clearable: true
    }
  },
  {
    field: 'jvmMemoryInUse',
    label: 'JVM内存使用量',
    value: '0',
    component: 'InputNumber',
    componentProps: {
      placeholder: '请选择JVM内存使用量',
      clearable: true
    }
  },
  // [[ OnflowGenerator Range (- inputSchema -) End ]]
  {
    field: 'createTime',
    label: '创建时间',
    component: 'Input',
    remove: isAdd.value,
    componentProps: {
      placeholder: '暂无创建时间',
      disabled: true
    }
  },
  {
    field: 'updateTime',
    label: '更新时间',
    component: 'Input',
    remove: isAdd.value,
    componentProps: {
      placeholder: '暂无更新时间',
      disabled: true
    }
  }
])

const rules = reactive({
  // [[ OnflowGenerator Range (- rules -) Start ]]
  infoId: [required()],
  machineId: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 50) {
          callback(new Error('服务器id不能超过50个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  cpuUsage: [required()],
  cpuFreq: [required()],
  memoryUsage: [required()],
  physicalMemoryInUse: [required()],
  physicalMemoryTotal: [required()],
  virtualMemoryInUse: [required()],
  virtualMemoryTotal: [required()],
  storageUsage: [required()],
  storageTotal: [required()],
  storageInUse: [required()],
  diskWrite: [required()],
  diskRead: [required()],
  networkDownload: [required()],
  networkUpload: [required()],
  jvmMemoryUsage: [required()],
  jvmMemoryTotal: [required()],
  jvmMemoryInUse: [required()]
  // [[ OnflowGenerator Range (- rules -) End ]]
})

defineExpose({
  submit
})
</script>

<template>
  <Form :rules="rules" @register="formRegister" :schema="schema" ref="formRef" />
</template>
